<?php
/**
 * 店舗情報用関数<br />
 *
 * @package apamanshop
 * @author　
 * @copyright　
 */
class Rstdtl_Model_Tenpodtl extends Apm_Model_Base {
	/**
	* 店舗詳細データ取得
	*/
	public function getTenpoDtlData(&$params)
	{
		$db_params = array();
		$sql = $this->generateSql($params, $db_params);
		return $this->dbGetList($sql, $db_params);
	}

	/**
	* 店舗詳細データを表示用に変換
	*
	* @access	public
	* @param	array
	* @param	array ※参照渡し
	* @return	void
	*/
	public function convertData(&$_config, &$tenpoData)
	{
		// Webcalling使用フラグ
		$webCallFlg = 0;
		// インスタンス生成
    	$loader = new Zend_Application_Module_Autoloader(array(
			'namespace' => 'Shop',
			'basePath' => APPLICATION_PATH . '/modules/shop',
		));
		// SHOPのBase.phpをインスタンス化
		$this->_model_shop  = new Shop_Model_Base();

		for($i=0;$i<count($tenpoData);$i++){
			// 住所
			$tenpoData[$i]["CONVERT_JYUUSHO"] = Apm_Bukkenitems::GetShopAddress($tenpoData[$i]);
			// 電話番号
			$tenpoData[$i]["CONVERT_TENPO_TEL01"] = Apm_Bukkenitems::GetShopTel($tenpoData[$i]);
			// 取引態様
			$tenpoData[$i]["CONVERT_TORIHIKI_TAIYOU"] = Apm_Bukkenitems::GetTorihikiTaiyouStr($tenpoData[$i]);
			// 店舗詳細URL
			$tenpoData[$i]["TENPO_DTL_URL"] = "'".substr($_config['apm']['baseurl'], 0, (strlen($_config['apm']['baseurl'])-1) ).Apm_Config_Shop::URL_DETAIL."','".$tenpoData[$i]["M05_TENPO_CD"]."','".$tenpoData[$i]["T03_HEYA_CD"]."'";
		}
	}

	/**
	* 同一部屋をもつ店舗詳細データ取得SQL
	*
	* @access	public
	* @param	array
	* @return	void
	*/
//	public function generateSql($params)
	public function generateSql($params, &$db_params)
	{
		$db_params['T03_HEYA_CD'] = $params['T03_HEYA_CD'];
		
		$sql = "";
//		$sql .= "SELECT";
		$sql .= "SELECT /*+ LEADING(T03) INDEX(T03 PK_T03WEB_HEYA) */";
		$sql .= "        T03_HEYA_CD";
		$sql .= "        ,T03_TORIHIKI_TAIYOU";
		$sql .= "        ,T03_MOTODUKE_FLAG";
		$sql .= "        ,T03_TENPO_DISTANCE";
		$sql .= "        ,M05_TENPO_CD";
		$sql .= "        ,M05_TENPO_NAME";
		$sql .= "        ,M05_JYUUSHO_TEXT";
		$sql .= "        ,M05_TENPO_BANCHI";
		$sql .= "        ,M05_TENPO_JYUUSHO_BIKOU";
		$sql .= "        ,M05_TENPO_TEL01";
		$sql .= "        ,M05_HOUZINMEI";
		$sql .= "        ,M05_MENKYO_HAKKOUMOTO";
		$sql .= "        ,M05_MENKYO_KOUSHINKAI";
		$sql .= "        ,M05_MENKYO_GOUSUU";
		$sql .= "        ,M05_TENPO_ANNAI_FLAG";
		$sql .= "        ,M05_HOUZIN_CD";
		$sql .= "        ,M93.*";
		$sql .= "    FROM";
//		$sql .= "        (";
//		$sql .= "            SELECT";
//		$sql .= "                    T03_HEYA_CD";
//		$sql .= "                    ,T03_TORIHIKI_TAIYOU";
//		$sql .= "                    ,T03_MOTODUKE_FLAG";
//		$sql .= "                    ,T03_TENPO_DISTANCE";
//		$sql .= "                    ,M05_TENPO_CD";
//		$sql .= "                    ,M05_TENPO_NAME";
//		$sql .= "                    ,M05_JYUUSHO_TEXT";
//		$sql .= "                    ,M05_TENPO_BANCHI";
//		$sql .= "                    ,M05_TENPO_JYUUSHO_BIKOU";
//		$sql .= "                    ,M05_TENPO_TEL01";
//		$sql .= "                    ,M05_HOUZINMEI";
//		$sql .= "                    ,M05_MENKYO_HAKKOUMOTO";
//		$sql .= "                    ,M05_MENKYO_KOUSHINKAI";
//		$sql .= "                    ,M05_MENKYO_GOUSUU";
//		$sql .= "                    ,M05_TENPO_ANNAI_FLAG";
//		$sql .= "                    ,M05_HOUZIN_CD";
//		$sql .= "                FROM";
//		$sql .= "                    T03WEB_HEYA";
//		$sql .= "                    ,M05TENPO_INFO";
//		$sql .= "                WHERE";
//		$sql .= "                    T03_TENPO_CD = M05_TENPO_CD";
//		$sql .= "                    AND (";
//		$sql .= "                        (T03_HEYA_CD = '".$params['T03_HEYA_CD']."')";
		//  一室表示解除(2010/12/14)
		//$sql .= "                        OR (";
		//$sql .= "                            T03_TENPO_CD <> '".$params['T03_TENPO_CD']."'";
		//$sql .= "                            AND T03_KOUKOKU_KAISHA_CD = '".$params['T03_KOUKOKU_KAISHA_CD']."'";
		//$sql .= "                            AND T03_KOUKOKU_TOIAWASE_NO = '".$params['T03_KOUKOKU_TOIAWASE_NO']."'";
		//$sql .= "                        )";
		// /一室表示解除(2010/12/14)
//		$sql .= "                    )";
//		$sql .= "        ) ";

		$sql .= "         T03WEB_HEYA T03";
		$sql .= "        ,M05TENPO_INFO";
		$sql .= '        ,M93CALLNOTE M93';		// 2013/08 WEBCALLING→CALLNOTE 切替
		$sql .= "    WHERE";
		$sql .= "            T03_HEYA_CD = :T03_HEYA_CD";
		$sql .= "        AND T03_TENPO_CD = M05_TENPO_CD";
		$sql .= "        AND T03_TENPO_CD = M93_TENPO_CD(+)";
//		if(array_key_exists("OemCd",$params) && $params['OemCd'] != ""){
//			// OEMコードが8桁（店舗）
//			if(strlen($params['OemCd']) == 8){
//				$sql .="     AND M05_TENPO_CD = '".$params['OemCd']."'";
//			// OEMコードが4桁（法人）
//			}else if(strlen($params['OemCd']) == 4){
//				$sql .="     AND M05_HOUZIN_CD = '".$params['OemCd']."'";
//			}
//		}
//		$sql .= "    ORDER BY";
//		$sql .= "        T03_MOTODUKE_FLAG DESC,T03_TENPO_DISTANCE";

		return $sql;
	}
}